package cn.iocoder.yudao.module.wms.service.rdrecord32.listener;

import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.wms.controller.admin.rdrecord32.vo.Rdrecord32SaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.rdrecord32.Rdrecord32DO;
import cn.iocoder.yudao.module.wms.enums.BpmnVariableConstants;
import cn.iocoder.yudao.module.wms.service.rdrecord32.Rdrecord32Service;
import cn.iocoder.yudao.module.wms.service.rdrecord32.Rdrecord32ServiceImpl;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 生产入库监听器
 *
 * @author 芋道源码
 */
@Component
public class Rdrecord32Listener extends BpmProcessInstanceStatusEventListener implements JavaDelegate {
    private static final Logger logger = LoggerFactory.getLogger(Rdrecord32Listener.class);
    @Resource
    private Rdrecord32Service rdrecord32Service;

    @Override
    protected String getProcessDefinitionKey() {
        return Rdrecord32ServiceImpl.PROCESS_KEY;
    }

    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        rdrecord32Service.updateRdrecord32Status(new Rdrecord32SaveReqVO().setProcessInstanceId(event.getId()).setStatus(event.getStatus()));
//        if (event.getStatus().equals(BpmnVariableConstants.BPM_APPROVED))
//        {
//            //通过流程ID获取主表信息
//            Rdrecord32DO rdrecord32DO=rdrecord32Service.findByProcessInstanceId(event.getId());
//            //获取子表信息
//            List<cn.iocoder.yudao.module.wms.dal.dataobject.rdrecords32.Rdrecords32DO> rdrecords32DOList=rdrecord32Service.getRdrecords32ListByPid(rdrecord32DO.getId());
//            //todo 对子表进行业务处理
//            rdrecord32Service.businessProcessing(rdrecord32DO,rdrecords32DOList);
//        }
    }

    @Override
    public void execute(DelegateExecution execution) {
        try {
            String reason = (String) execution.getVariable(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON);
        } catch (Exception e) {
            logger.error("Error in PomainStatusListener", e);
        }
    }
}
